package com.mzy.linear_struct.ks.动规;

/**
 * @Author: mizy3
 * @Date: 2024/08/22/上午11:41
 * @Description:
 */
public class 买卖股票的最佳时机 {

    /*public int maxProfit(int[] prices) {
        int ans = 0;
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < prices.length; i++) {
            min = Math.min(prices[i], min);
            ans = Math.max(ans, prices[i] - ans);
        }
        return ans;

    }

    public int maxProfit1(int[] prices) {
        // dp[i][0]表示第i天持有股票
        // dp[i][1]表示第i天不持有股票
        int[][] dp = new int[prices.length][2];

        dp[0][0] = prices[0];
        dp[0][1] = 0;

        for (int i = 1; i < prices.length; i++) {
            dp[i][0] = Math.min(dp[i - 1][0], prices[i]);
            dp[i][1] = Math.max(dp[i - 1][1], prices[i] - dp[i - 1][0]);
        }

        return dp[prices.length - 1][1];

    }*/

    public int maxProfit(int[] prices) {
        int ans = 0;
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < prices.length; i++) {
            min = Math.min(prices[i], min);
            ans = Math.max(prices[i] - min, ans);
        }

        return ans;
    }
}





















